/*
 * Copyright (c) 2023-2026, axenhook@qq.com
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-7-1      axenhook     first version
 */

#include <intrins.h>

#include "fsm.h"
#include "key.h"
#include "segment_display.h"
#include "piano.h"

void task_beep(fsm_task_id_t id, fsm_event_t event)
{
    if (event & FSM_EVENT_INIT)
    {
        fsm_set_delay_ms(id, 20);

        piano_init();
    }

    if (event & FSM_EVENT_DELAY)
    {
        u8 key;

        fsm_set_delay_ms(id, 20);

        key = key_get();
        if (key != KEY_INVALID)
        {
            key = KEY_GET_ID(key);
            if ((key == 0) || (key > 14))
            {
                piano_exit();
                fsm_destroy_task(id);
                return;
            }
        }
    }

    piano_play_music();
}
